<?
/*
 * Galeri, version 0.1
 * (c) 2007 Fajran Iman Rusadi.
 *
 * Galeri is freely distributable under the terms of an MIT-style license.
 *
 */


include('config.php');
include('common.php');

// Config

$tWidth = $_thumbnail_width;
$tHeight = $_thumbnail_height;

// Image

$img = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';

$pathinfo = getValidPath($img);
$img = $pathinfo['path'];

// Cache

$cacheFile = $_cache_dir . 'th_' . md5($img);
if (!file_exists($cacheFile)) {

	$path = $_images_dir . '/' . $img;
	
	$info = getimagesize($path);
	$width = $info[0];
	$height = $info[1];
	$type = $info['mime'];
	
	if ($width < $height) { $w = $h = $width; }
	else { $w = $h = $height; };
	
	$x = intval(($width - $w) / 2);
	$y = intval(($height - $h) / 2);
	
	$thumb = imagecreatetruecolor($tWidth, $tHeight);
	$orig = imagecreatefromstring(file_get_contents($path));
	imagecopyresampled($thumb, $orig, 0, 0, $x, $y, $tWidth, $tHeight, $w, $h);
	
	imagejpeg($thumb, $cacheFile, 60);
}

header('Content-type: image/jpeg');
print(file_get_contents($cacheFile));

?>
